package com.zhongkun.datahome.service.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.zhongkun.datahome.common.exception.ServiceException;
import com.zhongkun.datahome.common.utils.DateUtils;
import com.zhongkun.datahome.service.domain.CustomerHouse;
import com.zhongkun.datahome.service.domain.HouseBuildPlan;
import com.zhongkun.datahome.service.mapper.HouseBuildPlanMapper;
import com.zhongkun.datahome.service.repository.CustomerHouseRepository;
import com.zhongkun.datahome.service.repository.HouseBuildPlanRepository;
import com.zhongkun.datahome.service.req.cmd.plan.HouseBuildPlanCmd;
import com.zhongkun.datahome.service.service.IHouseBuildPlanService;
import com.zhongkun.datahome.service.utils.StreamUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 房产施工计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Service
public class HouseBuildPlanServiceImpl implements IHouseBuildPlanService {
    @Resource
    private HouseBuildPlanRepository houseBuildPlanRepository;

    @Resource
    private CustomerHouseRepository customerHouseRepository;

    @Override
    public List<HouseBuildPlan> list(Long houseId) {
        return houseBuildPlanRepository.listByHouseId(houseId);
    }

    @Override
    @Transactional
    public void save(HouseBuildPlanCmd cmd) {
        CustomerHouse customerHouse = customerHouseRepository.getById(cmd.getHouseId());
        if (Objects.isNull(customerHouse)) {
            throw new ServiceException("房产不存在");
        }

        if (!Objects.equals(customerHouse.getBuildPlanShowFlag(), cmd.getBuildPlanShowFlag())) {
            customerHouse.setBuildPlanShowFlag(cmd.getBuildPlanShowFlag());
            customerHouseRepository.updateById(customerHouse);
        }

        List<HouseBuildPlan> oldBuildPlanList = houseBuildPlanRepository.listByHouseId(cmd.getHouseId());
        if (CollectionUtils.isEmpty(cmd.getHouseBuildPlans())) {
            if (CollectionUtils.isNotEmpty(oldBuildPlanList)) {
                List<Long> toRemoveIds = oldBuildPlanList.stream().map(HouseBuildPlan::getId).collect(Collectors.toList());
                houseBuildPlanRepository.removeBatchByIds(toRemoveIds);
            }
            return;
        }

        Map<Boolean, List<HouseBuildPlan>> booleanAddMap = cmd.getHouseBuildPlans().stream()
                .collect(Collectors.partitioningBy(item -> Objects.isNull(item.getId())));

        Map<Long, HouseBuildPlan> updatePlanCmdMap = StreamUtil.listToMap(booleanAddMap.get(false), HouseBuildPlan::getId, e -> e);
        List<Long> toRemoveIds = new ArrayList<>();
        List<HouseBuildPlan> toUpdatePlanList = new ArrayList<>();

        oldBuildPlanList.forEach(oldPlan -> {
            if (updatePlanCmdMap.containsKey(oldPlan.getId())) {
                oldPlan.update(updatePlanCmdMap.get(oldPlan.getId()));
                toUpdatePlanList.add(oldPlan);
                return;
            }

            toRemoveIds.add(oldPlan.getId());
        });

        if (CollectionUtils.isNotEmpty(toRemoveIds)) {
            houseBuildPlanRepository.removeBatchByIds(toRemoveIds);
        }
        if (CollectionUtils.isNotEmpty(toUpdatePlanList)) {
            houseBuildPlanRepository.updateBatchById(toUpdatePlanList);
        }
        if (CollectionUtils.isNotEmpty(booleanAddMap.get(true))) {
            List<HouseBuildPlan> toAddPlanList = booleanAddMap.get(true).stream()
                    .map(item -> HouseBuildPlan.of(item, cmd.getHouseId()))
                    .collect(Collectors.toList());
            houseBuildPlanRepository.saveBatch(toAddPlanList);
        }
    }
}
